//
//  BKUserRealNameAuthReshultController.m
//  bestkeep
//
//  Created by windCould on 16/12/2.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKUserRealNameAuthReshultController.h"
#import "BKRealAuthFailReasonView.h"
#import "BKMyCommon.h"
#import "BKUserAuthRealInfoView.h"
#import "BKUserRealNameAuthController.h"
#import "BKUserSectionHeaderView.h"
#import "BKUserUnitRequestAPI.h"
@interface BKUserRealNameAuthReshultController ()

@property(nonatomic,strong)UIImageView *resultImageView;
@property(nonatomic,strong)UILabel * authResultLab;
@property(nonatomic,strong)BKRealAuthFailReasonView *reasonView;
@property(nonatomic,strong)UILabel * errorTipLab;
@property(nonatomic,strong)UILabel * reasonLab;
@property(nonatomic,strong)UILabel * disableSeenLab;
@property(nonatomic,strong)BKUserSectionHeaderView * personalInfHeaderView;

@property(nonatomic,strong)UIScrollView *mainScrollView;
@property(nonatomic,strong)UIView *functionView;

//个人信息
@property (nonatomic,strong)BKUserAuthRealInfoView * personalInfoView;



@end

@implementation BKUserRealNameAuthReshultController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"实名认证";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = [UIColor whiteColor];
    [self setUpViews];
  //  [self realNameAuthDetailData];
}

-(void)realNameAuthDetailData
{
    
    __weak typeof(self) weakSelf = self;
    [BKUserUnitRequestAPI getUserAuthRealNameDeatailWithCompleteBlock:^(id obj, NSError *error) {
        
        if (obj) {
            
            NSDictionary * dict = obj;
            NSNumber * isSuccess = dict[@"success"];
            NSInteger  numstr = [isSuccess integerValue];
            if (numstr == 1) {
                
                NSDictionary *dataDic = [obj objectForKey:@"data"];
                
                weakSelf.type = [[obj objectForKey:@"data"] objectForKey:@"realAuthStatus"];
                weakSelf.realName = [dataDic objectForKey:@"realName"];
                weakSelf.realPersonalId = [dataDic objectForKey:@"certypeNumber"];
                weakSelf.rejectReason = [dataDic objectForKey:@"reject"];
                weakSelf.realAuthStatusStr = [dataDic objectForKey:@"realAuthStatusStr"];
                
            } else {
            
                [HUD showErrorMessage:dict[@"msg"]];
                
            }
            
        } else {
    
            if (error.code==502) {
                [HUD showErrorMessage:@"服务器开小差了，请稍后再试！"];
            }else{
                [HUD showErrorMessage:error.localizedDescription];
            }
        }
    
    }];

}

#pragma mark =========================Function==============================

-(void)uploadAction
{
    BKUserRealNameAuthController * realNameAuthVC = [[BKUserRealNameAuthController alloc]init];
//    [self.navigationController popToViewController:realNameAuthVC animated:YES];
    [self.navigationController pushViewController:realNameAuthVC animated:YES];
}

#pragma mark =========================布局===================================

-(void)setUpViews{
    
    int type = [self.type intValue];
    
    _mainScrollView = [[UIScrollView alloc]init];
    _mainScrollView.backgroundColor = RGBCOLOR(242, 242, 242, 1);
    _mainScrollView.showsVerticalScrollIndicator = NO;
    _mainScrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_mainScrollView];
    _functionView = [[UIView alloc] init];
    _functionView.backgroundColor = [UIColor whiteColor];
    [_mainScrollView addSubview:_functionView];
    
    _resultImageView = [[UIImageView alloc]init];
    [_functionView addSubview:_resultImageView];
    
    _authResultLab = [UILabel new];
    _authResultLab.font = [UIFont systemFontOfSize:18];
    _authResultLab.text = self.realAuthStatusStr;
    _authResultLab.textColor = RGBCOLOR(64, 64, 64,1);
    _authResultLab.textAlignment = NSTextAlignmentCenter;
    [_functionView addSubview:_authResultLab];
    
    if (type == 1) {
        
        _resultImageView.image = [UIImage imageNamed:@"BK_My_pic_Complete_"];
        self.navigationController.navigationItem.rightBarButtonItem.accessibilityElementsHidden = YES;
    }else if (type == 2){
        
        _resultImageView.image = [UIImage imageNamed:@"BK_My_pic_underway_"];
    }else if (type == 3){
        
        _resultImageView.image = [UIImage imageNamed:@"BK_My_pic_defeated_"];
        UIBarButtonItem *rightBarBtnItem = [[UIBarButtonItem alloc]initWithTitle:@"重新上传" style:UIBarButtonItemStylePlain target:self action:@selector(uploadAction)];
        
        self.navigationItem.rightBarButtonItem = rightBarBtnItem;
        
    }else{
        
        _resultImageView.image = [UIImage imageNamed:@"BK_My_pic_defeated_"];
        UIBarButtonItem *rightBarBtnItem = [[UIBarButtonItem alloc]initWithTitle:@"重新上传" style:UIBarButtonItemStylePlain target:self action:@selector(uploadAction)];
        
        self.navigationItem.rightBarButtonItem = rightBarBtnItem;
    }
    
    _personalInfHeaderView = [[BKUserSectionHeaderView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 33)];
    _personalInfHeaderView.titleStr = @"身份信息";
    _personalInfHeaderView.titleLabel.textColor = [UIColor blackColor];
    _personalInfHeaderView.backgroundColor = BK_MY_COLOR_06;
    [_functionView addSubview:_personalInfHeaderView];
    
    
    _errorTipLab = [UILabel new];
    _reasonLab = [UILabel new];
    _disableSeenLab = [UILabel new];
    if (type == 3) {
        
        _errorTipLab.textColor = RGBCOLOR(136, 136, 136,1);
        _errorTipLab.font = [UIFont systemFontOfSize:12];
        _errorTipLab.text = @"失败原因：";
        [_functionView addSubview:_errorTipLab];
        
        NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc]init];
        paragraph.lineSpacing = 3;
        paragraph.paragraphSpacing = 3;
        paragraph.alignment = NSTextAlignmentLeft;
        NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:12],NSParagraphStyleAttributeName:paragraph};
        NSString *reasonStr = self.rejectReason;
        NSMutableAttributedString *contenStr = [[NSMutableAttributedString alloc]initWithString:reasonStr attributes:dic];
        paragraph.lineHeightMultiple = 1.0;
        _reasonLab.textColor = RGBCOLOR(136, 136, 136,1);
        _reasonLab.font = [UIFont systemFontOfSize:12];
        _reasonLab.attributedText = contenStr;
        _reasonLab.textAlignment = NSTextAlignmentLeft;
        _reasonLab.numberOfLines = 0;
        [_reasonLab sizeToFit];
        [_functionView addSubview:_reasonLab];
        
    }else{
        
        _disableSeenLab.textColor = RGBCOLOR(136, 136, 136,1);
        _disableSeenLab.font = [UIFont systemFontOfSize:12];
        _disableSeenLab.textAlignment = NSTextAlignmentCenter;
        _disableSeenLab.text = @"为了保护您的隐私，无法查看上传照片";
        [_functionView addSubview:_disableSeenLab];
        
    }
    
    _personalInfoView = [[BKUserAuthRealInfoView alloc]init];
    _personalInfoView.realNameStr = self.realName;
    _personalInfoView.identifiCardStr = self.realPersonalId;
    [_functionView addSubview:_personalInfoView];
    
    _reasonView = [[BKRealAuthFailReasonView alloc]init];
    [_functionView addSubview:_reasonView];
    
    
    [self layoutViews];
}

-(void)layoutViewsWithFrame {
    
    _mainScrollView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight-64);
    _resultImageView.frame = CGRectMake(ScreenWidth/2-25, 34, 50, 50);
    _authResultLab.frame = CGRectMake(0, _resultImageView.bottom+24, ScreenWidth, 15);
    CGFloat height;
    if ([self.type intValue] == 3) {
        
        _errorTipLab.frame = CGRectMake(40, _authResultLab.bottom+22, 65, 20);
        CGSize size = CGSizeMake(ScreenWidth-100-ApplicationBackGauge, MAXFLOAT);
        UIFont *font = [UIFont systemFontOfSize:12];
        height = [self.rejectReason boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size.height;
        _reasonLab.frame = CGRectMake(100, _authResultLab.bottom+24, ScreenWidth-100-ApplicationBackGauge, height);
        
    } else {
        
        CGSize size = CGSizeMake(ScreenWidth, MAXFLOAT);
        UIFont *font = [UIFont systemFontOfSize:12];
        height = [_disableSeenLab.text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size.height;
        _disableSeenLab.frame = CGRectMake(0, _authResultLab.bottom+24, ScreenWidth, height);
    }
    if ([self.type integerValue] == 3) {
        
        _personalInfHeaderView.frame = CGRectMake(0, _reasonLab.bottom+32, ScreenWidth, 30);
    }else{
        
        _personalInfHeaderView.frame = CGRectMake(0, _disableSeenLab.bottom+50, ScreenWidth, 30);
    }
    
    _personalInfoView.frame = CGRectMake(0, _personalInfHeaderView.bottom, ScreenWidth, 80);

    _reasonView.frame = CGRectMake(0, _personalInfoView.bottom, ScreenWidth, 120);
    _functionView.frame = CGRectMake(0, 0, ScreenWidth, _reasonView.bottom);
    _mainScrollView.contentSize = CGSizeMake(ScreenWidth,_reasonView.bottom);
}

-(void)layoutViews{
    
    [_mainScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    [_functionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(_mainScrollView);
        make.width.mas_equalTo(_mainScrollView);
    }];
    
    
    [_resultImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_functionView.mas_top).offset(34);
        make.centerX.mas_equalTo(_functionView.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(50, 50));
    }];
    
    [_authResultLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_resultImageView.mas_bottom).offset(24);
        make.left.mas_equalTo(_functionView.mas_left);
        make.right.mas_equalTo(_functionView.mas_right);
        make.height.mas_offset(@15);
    }];
    if ([self.type intValue] == 3) {
        
        [_errorTipLab mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_authResultLab.mas_bottom).offset(22);
            make.left.mas_equalTo(_functionView.mas_left).offset(40);
            make.height.mas_equalTo(@(20));
            make.width.mas_equalTo(@(65));
        }];
        
         [_reasonLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_authResultLab.mas_bottom).offset(24);
            make.left.mas_equalTo(_functionView.mas_left).offset(100);
            make.right.mas_equalTo(_functionView.mas_right).offset(-ApplicationBackGauge);
         }];
        
    } else {
        
        [_disableSeenLab mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_authResultLab.mas_bottom).offset(24);
            make.left.mas_equalTo(_functionView.mas_left);
            make.right.mas_equalTo(_functionView.mas_right);
        }];
        
    }
    
    
    [_personalInfHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        if ([self.type integerValue] == 3) {
            
            make.top.mas_equalTo(_reasonLab.mas_bottom).offset(32);
        }else{
            
            make.top.mas_equalTo(_disableSeenLab.mas_bottom).offset(50);
        }
        
        make.left.mas_equalTo(_functionView.mas_left);
        make.right.mas_equalTo(_functionView.mas_right);
        make.height.mas_offset(@(30));
    }];
    
    [_personalInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.personalInfHeaderView.mas_bottom).offset(0);
        make.left.mas_equalTo(_functionView.mas_left);
        make.right.mas_equalTo(_functionView.mas_right);
        make.height.mas_offset(@(80));
    }];
    
    [_reasonView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_personalInfoView.mas_bottom);
        make.left.mas_equalTo(_functionView.mas_left);
        make.right.mas_equalTo(_functionView.mas_right);
        make.height.mas_equalTo(120);
    }];
    [_functionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(_reasonView.mas_bottom);
    }];
}

@end
